package com.ailink.concurrent.queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created by Administrator on 2018/10/17
 */
public class BlockingQueueTest {

    public static void main(String[] args) {
        BlockingQueue<String> linkedBlockingDeque = new LinkedBlockingDeque<>();

        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                linkedBlockingDeque.add(String.valueOf(i));
            }
        });
        for (int i = 0; i < 1000; i++) {
            executorService.execute(() -> {
                try {
                    System.out.println(linkedBlockingDeque.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            executorService.execute(() -> {
                try {
                    System.out.println(linkedBlockingDeque.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        executorService.shutdown();

    }
}
